package com.example.yolov8android;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends Activity {
    private ImageView imageView;
    private Bitmap currentImage;
    private Detector detector;
    private static final String MODEL_PATH = "yolov8s_float32.tflite";
    private static final String LABELS_PATH = "labels.txt";
    private final ExecutorService processingExecutor = Executors.newSingleThreadExecutor();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // 初始化 UI
        imageView = findViewById(R.id.imageView);
        
        // 加载初始图片
        currentImage = BitmapFactory.decodeResource(getResources(), R.drawable.car);
        imageView.setImageBitmap(currentImage);
        
        // 初始化检测器
        detector = new Detector(getBaseContext(), MODEL_PATH, LABELS_PATH);
        detector.setup();
        
        // 开始检测
        if (currentImage != null) {
            processingExecutor.execute(() -> {
                List<BoundingBox> boundingBoxes = detector.detect(currentImage);
                final Bitmap updatedBitmap = drawBoundingBoxes(currentImage, boundingBoxes);
                runOnUiThread(() -> {
                    currentImage = updatedBitmap;
                    imageView.setImageBitmap(currentImage);
                });
            });
        }
    }

    private Bitmap drawBoundingBoxes(Bitmap bitmap, List<BoundingBox> boxes) {
        Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        Canvas canvas = new Canvas(mutableBitmap);
        
        Paint paint = new Paint();
        paint.setColor(Color.MAGENTA);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(8f);
        
        Paint textPaint = new Paint();
        textPaint.setColor(Color.rgb(0, 255, 0));
        textPaint.setTextSize(80f);
        textPaint.setTypeface(Typeface.DEFAULT_BOLD);

        // 计算比例因子，处理输入图像和检测图像尺寸不同的情况
        float scaleX = (float) mutableBitmap.getWidth() / detector.getInputWidth();
        float scaleY = (float) mutableBitmap.getHeight() / detector.getInputHeight();
        
        for (BoundingBox box : boxes) {
            // 根据实际图像大小调整检测框坐标
            RectF rect = new RectF(
                box.getX() * scaleX,
                box.getY() * scaleY,
                (box.getX() + box.getWidth()) * scaleX,
                (box.getY() + box.getHeight()) * scaleY
            );
            canvas.drawRect(rect, paint);
            canvas.drawText(box.getClsName(), rect.left, rect.bottom, textPaint);
        }

        return mutableBitmap;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        processingExecutor.shutdown();
    }
} 